package com.springcloud.demo.dynamicTable.autocode.utils;

import org.apache.ibatis.jdbc.SQL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SQLProvider {
    private Logger logger = LoggerFactory.getLogger("SQLProvider");

    public String getTableInfo(String tableName) {
        String result;
        // 为兼容不同数据库，别名统一使用小写+下划线分割
        if (DatabaseType.MYSQL.equals(DataTypeUtils.databaseType)) {
            // MySql
            result = new SQL() {{
                SELECT("table_name table_name, table_comment table_comment");
                FROM("information_schema.tables");
                WHERE("table_schema = (select database()) and table_name = '" + tableName + "'");
            }}.toString();
        } else if (DatabaseType.PG.equals(DataTypeUtils.databaseType)) {
            // PostgreSql
            result = new SQL() {{
                SELECT("relname table_name, col_description ( c.oid, 0 ) table_comment");
                FROM("pg_class c");
                WHERE("relkind = 'r' and relname NOT LIKE 'pg_%' and relname NOT LIKE 'sql_%' and relname = '" +
                        tableName + "'");
            }}.toString();
        } else {
            result = "";
        }

        logger.info(result);
        return result;
    }

    public String getColumnInfo(String tableName) {
        String result;
        // 为兼容不同数据库，别名统一使用小写+下划线分割
        if (DatabaseType.MYSQL.equals(DataTypeUtils.databaseType)) {
            //MySql
            result = new SQL() {{
                SELECT("column_name column_name, data_type column_type, column_comment column_desc, " +
                        "column_key column_key, extra");
                FROM("information_schema.columns");
                WHERE("table_schema = (select database()) and table_name = '" + tableName + "'");
                ORDER_BY("ordinal_position");
            }}.toString();
        } else if (DatabaseType.PG.equals(DataTypeUtils.databaseType)) {
            //PostgreSql
            result = new SQL() {{
                SELECT("a.attname AS column_name, t.typname AS column_type, d.description AS column_desc, " +
                        "(case when kcu.column_name is null then null else 'PRI' end) as column_key");
                FROM("pg_class c");
                LEFT_OUTER_JOIN("pg_attribute a on a.attrelid = c.oid");
                LEFT_OUTER_JOIN("pg_type t on a.atttypid = t.oid");
                LEFT_OUTER_JOIN("pg_description d ON a.attrelid = d.objoid AND a.attnum = d.objsubid");
                LEFT_OUTER_JOIN("information_schema.table_constraints AS tc ON tc.table_name = c.relname and tc.constraint_type = 'PRIMARY KEY'");
                LEFT_OUTER_JOIN("information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name and kcu.column_name = a.attname");
                WHERE("c.relname = '" + tableName + "' AND a.attnum > 0");
            }}.toString();
        } else {
            result = "";
        }

        logger.info(result);
        return result;
    }
}
